package com.erp.activity

import android.app.Activity
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.myAdapter.ChoiceAdapter
import com.erp.bean.CrmSouSuoBean
import com.erp.bean.StatusBean
import com.erp.Interface.OnUserDataInterface
import com.erp.module.crm.FragmentCrm
import com.erp.R
import com.erp.util.Httputil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_state.*
import kotlinx.android.synthetic.main.header_knoya.view.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2016/11/14/014.
 * 作者：刘洋
 */
class StateKActivity : BaseActivity(), View.OnClickListener, AdapterView.OnItemClickListener {

    private val TAG = "StateActivity"
    lateinit var abnormaladapter: ChoiceAdapter
    lateinit var handadapter: ChoiceAdapter
    lateinit var completeadapter: ChoiceAdapter
    lateinit var liststateabnormal: MutableList<StatusBean>
    lateinit var liststatehandad: MutableList<StatusBean>
    lateinit var liststatecomplete: MutableList<StatusBean>
    private var all = ""
    private var yichang = ""
    private var jingxing = ""
    private var wancheng = ""//总数数据
    lateinit var updatedialog: AlertDialog
    lateinit var souSuoBean : CrmSouSuoBean
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_state
    }

    override fun initCreate() {
        initView()
        inindata()
    }
    private fun initView() {
        souSuoBean = FragmentCrm.souSuoBean
        Header.iv_header_left.visibility = View.VISIBLE
        Header.tv_header_title.text = "状态查询"
        liststateabnormal = ArrayList()
        liststatehandad = ArrayList()
        liststatecomplete = ArrayList()
        abnormaladapter = ChoiceAdapter(liststateabnormal, this)
        handadapter = ChoiceAdapter(liststatehandad, this)
        completeadapter = ChoiceAdapter(liststatecomplete, this)
        LvAbnormal.adapter=abnormaladapter
        LvHand.adapter=handadapter
        LvComplete.adapter=completeadapter
        Header.iv_header_left.setOnClickListener(this)
        LlAll.setOnClickListener(this)
        LvAbnormal.onItemClickListener = this
        LvHand.onItemClickListener = this
        LvComplete.onItemClickListener = this
    }
    override fun onClick(v: View?) {
        when(v){
            Header.iv_header_left -> {
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
            LlAll -> {
                val intent = Intent()
                souSuoBean.status=""
                souSuoBean.statusname=""
                souSuoBean.all=""
                setResult(Activity.RESULT_OK, intent)
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
        }
    }
    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when(parent){
            LvAbnormal -> gonitify(position, liststateabnormal)
            LvHand ->  gonitify(position, liststatehandad)
            LvComplete -> gonitify(position, liststatecomplete)
        }
    }

    //    调动观察着，主界面更新数据
    private fun gonitify(i: Int, mlist: List<StatusBean>) {
        souSuoBean.status = "{\"" + mlist[i].filter_key + "\":true}"
        souSuoBean.statusname =  mlist[i].name
        val intent = Intent()
        setResult(Activity.RESULT_OK, intent)
        finish()
        overridePendingTransition(0, R.anim.end_activity1)
    }

    private fun inindata() {
        updatedialog = Myutil.showupdatedialog(this@StateKActivity, "正在加载", false)
        Httputil.getHttpState(this, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                updatedialog.dismiss()
                val states = data.split("<br />").toTypedArray()
                try {
                    val jo = JSONObject(states[states.size - 1])
                    all = jo.getJSONObject("all").getString("count")
                    yichang = jo.getJSONObject("yc").getString("count")
                    jingxing = jo.getJSONObject("ing").getString("count")
                    wancheng = jo.getJSONObject("complete").getString("count")
                    val js1 = jo.getJSONObject("yc").getJSONArray("status")
                    val js2 = jo.getJSONObject("ing").getJSONArray("status")
                    val js3 = jo.getJSONObject("complete").getJSONArray("status")
                    for (i in 0 until js1.length()) {
                        val jo1 = js1.get(i) as JSONObject
                        val statusBean = StatusBean()
                        statusBean.name = jo1.getString("name")
                        statusBean.count = jo1.getString("count")
                        statusBean.filter_key = jo1.getString("filter_key")
                        liststateabnormal.add(statusBean)
                    }
                    for (i in 0 until js2.length()) {
                        val jo1 = js2.get(i) as JSONObject
                        val statusBean = StatusBean()
                        statusBean.name = jo1.getString("name")
                        statusBean.count = jo1.getString("count")
                        statusBean.filter_key = jo1.getString("filter_key")
                        liststatehandad.add(statusBean)
                    }
                    for (i in 0 until js3.length()) {
                        val jo1 = js3.get(i) as JSONObject
                        val statusBean = StatusBean()
                        statusBean.name = jo1.getString("name")
                        statusBean.count = jo1.getString("count")
                        statusBean.filter_key = jo1.getString("filter_key")
                        liststatecomplete.add(statusBean)
                    }
                } catch (e: JSONException) {
                    e.printStackTrace()
                }

                TvAll.text = all
                TvJingxing.text = jingxing
                TvWancheng.text = wancheng
                TvYichang.text = yichang
                abnormaladapter.notifyDataSetChanged()
                handadapter.notifyDataSetChanged()
                handadapter.notifyDataSetChanged()
            }

            override fun onNot(throwable: String) {
                updatedialog.dismiss()
            }
        })
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish()
            overridePendingTransition(0, R.anim.end_activity1)
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object {
        private val a = "StateActivity"
    }
}
